/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package suit.ui;

/**
 * A Widget that displays the progress of a task as a visual bar. This is
 * principally used to give feedback to a user that things are actually
 * happening when some computationally intensive or long running task is taking
 * place. You can use a ProgressBar in two ways. Usually you know what fraction
 * of a task is complete and that is what you want to display as a steadily
 * increasing bar. On the other hand, if the total duration of the task is
 * unknown or unpredictable, you can still indicate progress is being made by
 * having the bar "pulse" back and forth. (Pulse is not implemented)
 * 
 * @author Devin Samarin
 * 
 */
public class ProgressBar extends Widget {

	// private int orient;
	private double fraction = 0;

	private Label label = new Label();

	/**
	 * Update progress from top to bottom (not implemented)
	 */
	public static final int TOPDOWN = 1;
	/**
	 * Update progress from bottom to top (not implemented)
	 */
	public static final int BOTTOMUP = 2;
	/**
	 * Update progress from left to right (default)
	 */
	public static final int LEFTRIGHT = 4;
	/**
	 * Update progress from right to left (not implemented)
	 */
	public static final int RIGHTLEFT = 8;

	/**
	 * Construct a new ProgressBar with the specified text
	 * 
	 * @param label
	 */
	public ProgressBar(String label) {
		this.label.setText(label);
	}

	/**
	 * Construct a new ProgressBar without any text inside
	 */
	public ProgressBar() {
	}

	protected boolean onExpose(Event e) {
		
		e.theme.drawProgressBorder(e.graphics, this, allocation);
		e.theme.drawProgressBar(e.graphics, this, new Rectangle(allocation.x, allocation.y,
				(int) (allocation.w * fraction), allocation.h));

		label.sizeAllocate(allocation);
		label.registerEvent(e);
		return false;
	}

	/**
	 * Get the current progress bar fraction (0-1)
	 * 
	 * @return fraction
	 */
	public double getFraction() {
		return fraction;
	}

	/**
	 * Set the fraction of the ProgressBar that shows as completed or
	 * "filled-in". If fraction is greater than 1 or less than 0, it will be
	 * clipped.
	 * 
	 * @param fraction
	 *            amount progressed from 0 - 1
	 */
	public void setFraction(double fraction) {
		if (fraction > 1)
			fraction = 1;
		if (fraction < 0)
			fraction = 0;
		this.fraction = fraction;
		invalidate(this);
	}

	/**
	 * Return the text of the label
	 * 
	 * @return label text
	 */
	public String getText() {
		return label.getText();
	}

	/**
	 * Set the text of the label
	 * 
	 * @param text
	 *            label text
	 */
	public void setText(String text) {
		this.label.setText(text);
		invalidate(this);
	}

	protected Rectangle getRequisition() {
		Rectangle r = label.getRequisition();
		r.w += 10;
		r.h += 10;
		return r;
	}

}
